package org.igeek.atomqq.service;

import org.igeek.atomqq.task.MessagePoll;
import org.igeek.atomqq.vo.QQMessage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/** 
 * 消息轮询广播接收
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-10 下午09:57:26 
 */
public class MessageReceiver extends BroadcastReceiver {
	public static final String BROADCAST_TYPE="broadcast_type";
	
	public static final String PERMISSION="org.igeek.atomqq.MESSAGE"; 
	
	public final static int BROADCAST_TYPE_FRIEND=0x1;
	public final static int BROADCAST_TYPE_FRIEND_STATUS=0x2;
	public final static int BROADCAST_TYPE_FRIEND_LISTCHG=0x3;
	public final static int BROADCAST_TYPE_GROUP=0x4;
	public final static int BROADCAST_TYPE_SYS_GROUP=0x5;
	
	private OnMessageRecevied messageHandle;
	
	public MessageReceiver(OnMessageRecevied messageHandle) {
		this.messageHandle=messageHandle;
	}
	

	@Override
	public void onReceive(Context context, Intent intent) {
		
		int type=intent.getIntExtra(BROADCAST_TYPE,0);
		
		switch (type) {
		case BROADCAST_TYPE_FRIEND:
			
			QQMessage messageFriend=(QQMessage) intent.getSerializableExtra(MessagePoll.BROADCAST_MSG_FRIEND);
			messageHandle.handleMessage(BROADCAST_TYPE_FRIEND,messageFriend);
			
			break;

		case BROADCAST_TYPE_FRIEND_STATUS:
			
			QQMessage messageFriendSts=(QQMessage) intent.getSerializableExtra(MessagePoll.BROADCAST_MSG_FRIEND_STATUS);
			messageHandle.handleMessage(BROADCAST_TYPE_FRIEND_STATUS,messageFriendSts);
			break;
			
		case BROADCAST_TYPE_FRIEND_LISTCHG:
			
			QQMessage messageFriendListChg=(QQMessage) intent.getSerializableExtra(MessagePoll.BROADCAST_MSG_FRIEND_LSTCHG);
			messageHandle.handleMessage(BROADCAST_TYPE_FRIEND_LISTCHG,messageFriendListChg);
			break;
			
		case BROADCAST_TYPE_GROUP:
			
			QQMessage messageGroup=(QQMessage) intent.getSerializableExtra(MessagePoll.BROADCAST_MSG_GROUP);
			messageHandle.handleMessage(BROADCAST_TYPE_GROUP,messageGroup);
			break;
			
		case BROADCAST_TYPE_SYS_GROUP:
			
			QQMessage messageSysGroup=(QQMessage) intent.getSerializableExtra(MessagePoll.BROADCAST_MSG_SYS_GROUP);
			messageHandle.handleMessage(BROADCAST_TYPE_SYS_GROUP,messageSysGroup);
			break;

		}
		
		
	}
	
	/**
	 * 接收消息时处理
	 * @author hangxin1940@gmail.com
	 * @link http://hangxin1940.cnblogs.com
	 * @time 2012-2-10 下午09:58:56
	 *
	 */
	public interface OnMessageRecevied{
		
		public void handleMessage(int type,QQMessage message);
		
	}
}
